package com.shanghaionstar.user.presenter;

import android.content.Context;
import android.os.Bundle;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.PreferencesUtls;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.smart.BaseResponse;
import com.onstar.cn.smart.entity.ServiceItem;
import com.shanghaionstar.OnstarApplication;
import com.shanghaionstar.user.iview.IOptView;
import com.shanghaionstar.user.service.OptService;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.utils.JoinMeConstants;

import java.util.List;

import rx.functions.Action1;

/**
 * Created by Yonglang Hong on 2016/9/26.
 */
public class OptPresenter extends BasePresenter {

    /**
     * 驾驶行为评价服务
     */
    public static final String SMART_DRIVE = "SmartDrive";

    /**
     * 车辆报告数据分享
     */
    public static final String CAR_ASSESSMENT = "CarAssessment";
    public static final String FUEL_ECONOMY = "FuelEconomy";
    public static final String SMART_HOME = "SmartHome";


    private IOptView optView;

    private OptService optService;

    private Context context;

    public OptPresenter(IMVPScheduler mvpScheduler, Context context) {
        super(mvpScheduler);
        optService = new OptService(context);
        this.context = context;
    }


    public ServiceItem getServiceItemByName(String name) {
        List<ServiceItem> serviceItems = ApplicationUtil.getInstance().getServiceItems();
        if (serviceItems != null && !serviceItems.isEmpty()) {
            for (ServiceItem item : serviceItems) {
                if (item.getServiceName().equalsIgnoreCase(name)) {
                    return item;
                }
            }
        }
        return null;
    }

    public void setOptView(IOptView optView) {
        this.optView = optView;
    }

    //
    public void turnOn(final Bundle bundle) {
        subscription = optService.optInService(SMART_DRIVE)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<BaseResponse>() {
                    @Override
                    public void call(BaseResponse response) {
                        ServiceItem item = getServiceItemByName(SMART_DRIVE);
                        item.turnOn();
                        if (optView != null) {
                            if (bundle == null) {
                                //驾驶行为评分重新打开
                                PreferencesUtls.getInstance(context).putString(
                                        (FinalConstants.SharedPreferencesConfig.ISFIRSTINFO + ApplicationUtil.getInstance().getIdpID()
                                                + ApplicationUtil.getInstance().getVin()), "n");//e表示第1+次打开；y代表第1次打开；n代表设置里关闭再打开
                                optView.onTurnOnSuccess(response);
                            } else {
                                optView.onTurnOnSuccess(response, bundle);

                            }
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        if (optView != null && throwable != null) {
                            throwable.printStackTrace();
                            optView.onFailed(throwable);
                        }
                    }
                });
    }

    public void optIn(final String serviceName) {
        subscription = optService.optInService(serviceName)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<BaseResponse>() {
                    @Override
                    public void call(BaseResponse response) {
                        if (optView != null) {
                            optView.onSwitchService(serviceName, true, true, null);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        if (optView != null && throwable != null) {
                            throwable.printStackTrace();
                            optView.onSwitchService(serviceName, true, false, throwable);
                        }
                    }
                });
    }

    public void optOut(final String serviceName) {
        subscription = optService.optOutService(serviceName)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<BaseResponse>() {
                    @Override
                    public void call(BaseResponse response) {
                        if (optView != null) {
                            optView.onSwitchService(serviceName, false, true, null);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        if (optView != null && throwable != null) {
                            throwable.printStackTrace();
                            optView.onSwitchService(serviceName, false, false, throwable);
                        }
                    }
                });
    }

//    public void turnOff() {
//        subscription = optService.optOutService(SMART_DRIVE)
//                .subscribeOn(mvpScheduler.backgroundThread())
//                .observeOn(mvpScheduler.mainThread())
//                .subscribe(new Action1<BaseResponse>() {
//                    @Override
//                    public void call(BaseResponse response) {
//                        if (optView != null) {
//                            optView.onTurnOffSuccess(response);
//                        }
//                    }
//                }, new Action1<Throwable>() {
//                    @Override
//                    public void call(Throwable throwable) {
//                        //里面抛出的Error信息
//                        if (optView != null && throwable != null) {
//                            throwable.printStackTrace();
//                            optView.onFailed(throwable);
//                        }
//                    }
//                });
//    }

//    public void turnOnCarAssess() {
//        subscription = optService.optInService(CAR_ASSESSMENT)
//                .subscribeOn(mvpScheduler.backgroundThread())
//                .observeOn(mvpScheduler.mainThread())
//                .subscribe(new Action1<BaseResponse>() {
//                    @Override
//                    public void call(BaseResponse response) {
//                        ServiceItem item = OnstarApplication.getApplication().getCarAssessStatusItem();
//                        if (item != null)
//                            item.turnOn();
//                        if (optView != null) {
//                            optView.onTurnOnCarAssessSuccess(response);
//                        }
//                    }
//                }, new Action1<Throwable>() {
//                    @Override
//                    public void call(Throwable throwable) {
//                        //里面抛出的Error信息
//                        if (optView != null && throwable != null) {
//                            throwable.printStackTrace();
//                            optView.onTurnCarAssessFailed(throwable);
//                        }
//                    }
//                });
//    }
//
//    public void turnOffCarAssess() {
//        subscription = optService.optOutService(CAR_ASSESSMENT)
//                .subscribeOn(mvpScheduler.backgroundThread())
//                .observeOn(mvpScheduler.mainThread())
//                .subscribe(new Action1<BaseResponse>() {
//                    @Override
//                    public void call(BaseResponse response) {
//                        ServiceItem item = OnstarApplication.getApplication().getCarAssessStatusItem();
//                        if (item != null)
//                            item.turnOff();
//                        if (optView != null) {
//                            optView.onTurnOffCarAssessSuccess(response);
//                        }
//                    }
//                }, new Action1<Throwable>() {
//                    @Override
//                    public void call(Throwable throwable) {
//                        //里面抛出的Error信息
//                        if (optView != null && throwable != null) {
//                            throwable.printStackTrace();
//                            optView.onTurnCarAssessFailed(throwable);
//                        }
//                    }
//                });
//    }


//       public void getCarAssessStatus(final Context context) {
//        addSubscription(optService.getServiceStatus(CAR_ASSESSMENT).subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
//                .subscribe(new Action1<List<ServiceItem>>() {
//                    @Override
//                    public void call(List<ServiceItem> items) {
//                        ServiceItem item = items.get(0);
//                        OnstarApplication.getApplication().setCarAssessStatusItem(item);
//                        if (ServiceItem.isCarAssessAvailable(item)) {
//                            String loginRole = ApplicationUtil.getInstance().getLoginRole();
//                            boolean isOwner = !CommonUtil.isNull(loginRole) && loginRole.equalsIgnoreCase(JoinMeConstants.OWNER);
//                            if (optView != null && context != null && isOwner)
//                                optView.showCarAssessAgreement(true);
//                        } else {
//                            if (optView != null && context != null)
//                                optView.showCarAssessAgreement(false);
//                        }
//                    }
//                }, new Action1<Throwable>() {
//                    @Override
//                    public void call(Throwable throwable) {
//                        if (optView != null && context != null)
//                            optView.showCarAssessAgreement(false);
//                    }
//                }));
//    }
//

    public void getServiceStatus(final Context context) {
        addSubscription(optService.getServiceStatus("").subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<List<ServiceItem>>() {
                    @Override
                    public void call(List<ServiceItem> items) {
                        if (items != null && !items.isEmpty()) {
                            ApplicationUtil.getInstance().setServiceItems(items);
                            optView.onGetServiceItem(items);
                        } else {
                            optView.onGetServiceItem(null);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        if (optView != null && context != null)
                            optView.onGetServiceItem(null);
                    }
                }));
    }

}
